package com.teach.controller.shop;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.teach.dto.LoginUser;
import com.teach.entity.ShopAddress;
import com.teach.mapper.ShopAddressMapper;
import com.teach.service.shop.ShopAddressService;

import lombok.extern.slf4j.Slf4j;

@Controller
@Slf4j
public class ShopAddressController {

	@Autowired
	private ShopAddressService service;

	private ShopAddressMapper mapper;

	// 收货地址管理
//	@GetMapping("/userAddress.page")
//	public String doAddresslist() {
//		return "shop/userAddress";
//	}
	
//	@GetMapping("/userAddress.page")
	@RequestMapping("/userAddress.page")
	public String doAddress(Model model, LoginUser user) {
		model.addAttribute("addresslist", service.get(user.getUserName()));
		return "shop/userAddress";
	}
	
//	@RequestMapping("/address.do")
//	@ResponseBody
//	public String doAddress(Model model, LoginUser user) {
//		model.addAttribute("addresslist", service.get(user.getUserName()));
////		return "shop/userAddress";
//		return "redirect:/address.do";
//	}
		
	// 添加收货地址
	@GetMapping("/addAddress.page")
	public String doAddAddress() {
		return "shop/userAddAddress";
	}

//	@RequestMapping("/address.do")
////	@ResponseBody
//	public String addressUserName(Model model, LoginUser user) {
//		return "/shop/userAddress";
//	}

	// 设置默认地址
//	@PostMapping("/setDefault.address")
//	@ResponseBody
//	public String doUpdate(Integer msg, Integer id) {
//		ShopAddress upd = new ShopAddress();
//		service.updateMsg(msg, id);
////		return "/shop/userAddress";
//		return "redirect:/setDefault.address";
//	}

	@PostMapping("/setDefault.address")
	@ResponseBody
	public String doUpdate(Integer msg, Integer id, LoginUser user) { 
		//先将该账号下的地址都设为不默认0
		service.update(user.getUserName());
		//将要修改的地址设为默认
		service.updateMsg(msg=1, id);
		//修改完查询
//		service.get(user.getUserName());
		return "redirect:/setDefault.address";
	}

	// 删除地址
//	@RequestMapping("/delete.address")
//	@ResponseBody
//	public String deleteAddress(Integer id, LoginUser user) {
//		ShopAddress sel = new ShopAddress();
//		sel.setShopAddressId(id);
//		service.delete(id);
//		return "redirect:/delete.address";
//	}

	@PostMapping("/delete.address")
	@ResponseBody
	public String deleteAddress(Integer id) {
		service.delete(id);
		return "redirect:/delete.address";
	}

//	@RequestMapping("/delete.address")
//	@ResponseBody
//	public String deleteAddress(Integer addressId, LoginUser user) {
//		ShopAddress id = service.select(addressId);
//		addressId = id.getShopAddressId();
//		service.del(addressId, user.getUserName());
//		log.debug("{}" + addressId);
//		return "redirect:/delete.address";
//	}

//	@RequestMapping("/delete.address")
//	public String dodelect(@RequestParam(value = "shopAddressId") Integer shopAddressId) {
//		ShopAddress id = service.select(shopAddressId);
//		shopAddressId = id.getShopAddressId();
//		service.delete(shopAddressId);
//		log.debug("{}" + shopAddressId);
//		return "redirect:/delete.address";
//	}

	// 添加地址
	@PostMapping("/add.address")
	@ResponseBody
	public Object doAdd(String province, String city, String area,
			String posi, String pho, String rel, Integer msg, LoginUser userName) {
		
//		ShopAddress add = new ShopAddress();
//		add.setShopAddressProvince(province);
//		add.setShopAddressCity(city);
//		add.setShopAddressArea(area);
//		add.setShopAddressUserAddress(posi);
//		add.setShopAddressUserTel(pho);
//		add.setShopAddressRealName(rel);
//		add.setShopAddressMsg(msg);
//		add.setShopAddressUserName(userName.getUserName());
		
//		ShopAddress add = (ShopAddress) mapper.selectByUserName(userName.getUserName());
//		List<ShopAddress> add = service.get(userName.getUserName());
//		ShopAddress add = service.getMsg(msg,userName.getUserName());
		//先查询，如果有状态为1,就先都设为0
//		if(add.getShopAddressMsg()==1) {
//			service.update(userName.getUserName());
//			service.insert(province, city, area, posi, pho, rel, msg, userName.getUserName());
//		}
//		if(add.getShopAddressMsg()==0) {
//			service.insert(province, city, area, posi, pho, rel, msg, userName.getUserName());
//		}
		
		//先将该账号下的地址都设为不默认0
		service.update(userName.getUserName());
		service.insert(province, city, area, posi, pho, rel, msg, userName.getUserName());
		return "redirect:/add.address";
	}

}